package com.slf.engine.threads;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.mail.EmailException;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.biz.IFcjs0573Biz;
import com.slf.engine.bo.TFcinfo;
import com.slf.engine.common.SysContext;
import com.slf.engine.mail.SendMail;
import com.slf.engine.services.IBusService;
import com.slf.engine.services.IFcjs0573Service;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.services.impl.Fcjs0573ServiceImpl;
import com.slf.engine.utils.Factory;

public class SendThread extends Thread {

	private static final Logger log = Logger.getLogger(SendThread.class);
	public SendThread()
	{
		super("SEND_THREAD");
	}
	
	public void run()
	{
		IBusService busService = new BusServiceImpl();
		while(true)
		{
			if(SysContext.IS_START)
			{
				List<TFcinfo> list = busService.getUnSendFcInfo();
				if(list != null && !list.isEmpty())
				{
					String body = "";
					String title = "";
					try {
						IFcjs0573Biz fcjs0573Biz = (IFcjs0573Biz)Factory.getInstantBiz("fcjs0573Biz");
						body = fcjs0573Biz.makeEmailBody(list);
						title = fcjs0573Biz.makeEmailTitle(list);
						log.log(Priority.INFO, "开始发送邮件");
						SendMail mail = new SendMail();
						mail.sendHtmlEmail(title,body).send();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						log.log(Priority.ERROR, e.getMessage());
					} catch (EmailException e) {
						// TODO Auto-generated catch block
						log.log(Priority.ERROR, e.getMessage());
					}
				}else{
					try {
						this.sleep(10000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					}
				}
			}else{
				try {
					this.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
				}
			}
		}
	}
}
